package cn.hyp.bantang.module.finance.service.baselr;

import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import cn.hyp.bantang.module.finance.controller.admin.baselr.vo.BaseLrPageReqVO;
import cn.hyp.bantang.module.finance.controller.admin.baselr.vo.BaseLrSaveReqVO;
import cn.hyp.bantang.module.finance.dal.dataobject.baselr.BaseLrDO;
import cn.hyp.bantang.module.finance.dal.mysql.baselr.BaseLrMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.hyp.bantang.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hyp.bantang.module.finance.enums.ErrorCodeConstants.BASE_LR_NOT_EXISTS;

/**
 * 个股报告期利润 Service 实现类
 *
 * @author 半唐
 */
@Service
@Validated
public class BaseLrServiceImpl implements BaseLrService {

    @Resource
    private BaseLrMapper baseLrMapper;

    @Override
    public Long createBaseLr(BaseLrSaveReqVO createReqVO) {
        // 插入
        BaseLrDO baseLr = BeanUtils.toBean(createReqVO, BaseLrDO.class);
        baseLrMapper.insert(baseLr);
        // 返回
        return baseLr.getId();
    }

    @Override
    public void updateBaseLr(BaseLrSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseLrExists(updateReqVO.getId());
        // 更新
        BaseLrDO updateObj = BeanUtils.toBean(updateReqVO, BaseLrDO.class);
        baseLrMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseLr(Long id) {
        // 校验存在
        validateBaseLrExists(id);
        // 删除
        baseLrMapper.deleteById(id);
    }

    private void validateBaseLrExists(Long id) {
        if (baseLrMapper.selectById(id) == null) {
            throw exception(BASE_LR_NOT_EXISTS);
        }
    }

    @Override
    public BaseLrDO getBaseLr(Long id) {
        return baseLrMapper.selectById(id);
    }

    @Override
    public PageResult<BaseLrDO> getBaseLrPage(BaseLrPageReqVO pageReqVO) {
        return baseLrMapper.selectPage(pageReqVO);
    }

}